/**
 * This project create for studying propose at BUPT University. You can change or reuse this project anywhere
 */

package edu.bupt.ooad.library.ui;

import java.util.List;

import javax.swing.table.AbstractTableModel;

import edu.bupt.ooad.library.Patron;

/**
 * @author Hamed Abdollahpour
 */
public class PatronTableModel extends AbstractTableModel {

    private final static String[] COLUMNS = new String[] {
            "Type", "Name", "ID"
    };

    private List<Patron> patrons = null;

    public PatronTableModel(List<Patron> petrons) {
        this.patrons = petrons;
    }

    public Patron getPatron(int row) {
        return patrons.get(row);
    }

    public void removeRow(int row) {
        patrons.remove(row);

        fireTableRowsDeleted(row, row);
    }

    @Override
    public int getColumnCount() {
        return COLUMNS.length;
    }

    @Override
    public String getColumnName(int column) {
        return COLUMNS[column];
    }

    @Override
    public int getRowCount() {
        return patrons.size();
    }

    @Override
    public Object getValueAt(int row, int column) {
        Patron petron = patrons.get(row);

        switch (column) {
            case 0:
                String type = "Unkown";
                if (petron.getType() == Patron.PetronType.STUDENT) {
                    type = "Student";
                } else if (petron.getType() == Patron.PetronType.TEACHER) {
                    type = "Teacher";
                }
                return type;

            case 1:
                return petron.getLastname() + " " + petron.getFirstname();

            case 2:
                return petron.getId();

            default:
                return null;
        }
    }

}
